import 'package:flutter/material.dart';
import 'package:fwanandroid/model/api/WanAndroidService.dart';
import 'package:fwanandroid/ui/MyColors.dart';

class SearchPage extends StatefulWidget {
  const SearchPage({super.key});

  @override
  State<SearchPage> createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  var _searchKeys = <Widget>[];
  var _webs = <Widget>[];

  @override
  void initState() {
    super.initState();
    loadData();
  }

  @override
  Widget build(BuildContext context) {
    if (_searchKeys.isEmpty && _webs.isEmpty) {
      return Container();
    } else {
      return Scaffold(
        appBar: AppBar(
          backgroundColor: MyColors.leiMuBlue,
          leading: InkWell(
              onTap: () {
                Navigator.of(context).pushNamed('/searchResult');
              },
              child: Icon(
                Icons.search,
                color: Colors.white,
              )),
        ),
        body: CustomScrollView(
          slivers: [
            SliverToBoxAdapter(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text(
                      "大家都在搜",
                      style: TextStyle(fontSize: 16),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 8),
                    child: Wrap(
                      spacing: 16,
                      runSpacing: 16,
                      direction: Axis.horizontal,
                      children: _searchKeys,
                    ),
                  )
                ],
              ),
            ),
            SliverToBoxAdapter(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text(
                      "常用网站",
                      style: TextStyle(fontSize: 16),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 8),
                    child: Wrap(
                      spacing: 16,
                      runSpacing: 16,
                      direction: Axis.horizontal,
                      children: _webs,
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      );
    }
  }

  void loadData() {
    WanAndroidService.getHot().then((value) {
      setState(() {
        _searchKeys = value.map((item) {
          return InkWell(
              onTap: () {
                Navigator.of(context)
                    .pushNamed('/searchResult', arguments: "${item.name}");
              },
              child: Text(item.name, style: TextStyle(color: Colors.grey)));
        }).toList();
      });
    });
    WanAndroidService.getWebsites().then((value) {
      setState(() {
        _webs = value.map((item) {
          return InkWell(
              onTap: () {
                Navigator.of(context)
                    .pushNamed('/searchResult', arguments: "${item.name}");
              },
              child: Text(item.name, style: TextStyle(color: Colors.grey)));
        }).toList();
      });
    });
  }
}
